package com.wkq.move.module.main.frame.view

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.lib_base.adapter.KtDataBindingAdapter
import com.wkq.lib_base.utlis.AlertUtil
import com.wkq.move.R
import com.wkq.move.module.main.ui.adapter.HomeAdapter
import com.wkq.move.module.main.ui.adapter.TvShowAdapter
import com.wkq.move.module.main.ui.fragment.TvListFragment
import com.wkq.move.module.tv.ui.activity.UsTvShowActivity
import com.wkq.net.model.TvListsInfo
import com.wkq.net.model.USTVShowInfo
import java.util.regex.Pattern

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/5/22
 *
 * 用途:
 */


class TvListView : MvpView {

    var mFragment: TvListFragment
    var mAdapter: TvShowAdapter? = null

    constructor(tvListView: TvListFragment) {
        mFragment = tvListView
    }

    fun initView() {
        mAdapter = TvShowAdapter(mFragment.activity as Context, R.layout.item_tv_show)
        mFragment.binding.rvContent.layoutManager = GridLayoutManager(mFragment.activity, 3)
        mFragment.binding.rvContent.adapter = mAdapter

        mFragment.binding.sfLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (!TextUtils.isEmpty(getNexPage(mFragment.index))) {
                    mFragment.presenter.getData(getNexPage(mFragment.index))
                } else {
                    mFragment.binding.sfLayout.finishRefresh()
                    mFragment.binding.sfLayout.finishLoadMore()
                }

            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                mFragment.presenter.getData(mFragment.startIndex)
            }

        })

        mAdapter!!.setOnViewClickListener(object :
            KtDataBindingAdapter.OnAdapterViewClickListener<USTVShowInfo> {
            override fun onViewClick(v: View?, program: USTVShowInfo?) {
                UsTvShowActivity.Companion.newInstance(
                    mFragment.activity as Context,
                    program?.tvSite
                )
            }
        })

    }

    fun showInfos(infos: TvListsInfo?) {

        mFragment.totalIndex = infos?.totalIndex
        if (!TextUtils.isEmpty(getNexPage(mFragment.index)))  mFragment.index = getNexPage(mFragment.index)
        mAdapter?.addItems(infos?.tvList?.toMutableList())
        mFragment.binding.sfLayout.finishRefresh()
        mFragment.binding.sfLayout.finishLoadMore()
    }

    fun hasNext(currentIndex: String?): Boolean {
        var p = Pattern.compile("\\d+")
        var p2 = Pattern.compile("\\d+")
        var mToTal = p.matcher(mFragment.totalIndex)

        var index = p2.matcher(currentIndex)
        mToTal.find()
        index.find()

        return index.group().toInt() < mToTal.group().toInt()
    }

    fun getNexPage(nowPage: String?): String {

        if (hasNext(nowPage)) {
            var p = Pattern.compile("\\d+")
            var m = p.matcher(nowPage)
            m.find()

            var page = m.group().toInt();

            var newpage = (page + 1)

            return nowPage!!.replace(page.toString(), newpage.toString())
        }

        return ""

    }

    fun showMessage(message: String?) {
        if (mFragment == null || mFragment.activity == null || TextUtils.isEmpty(message)) return;
        AlertUtil.showDeftToast(mFragment.activity, message)

    }

}

